package ECCcliente;

import java.io.*;
import java.net.*;
public class Ventana extends javax.swing.JFrame implements Runnable {
    
    int a,b,p,x,y,privada,k;
    int Pmx=-1, Pmy=-1;
    int Cifrado[][] = new int[2][2];
    int Descifrado[] = new int[2];
    ECC cifrador= new ECC();
    
    DataInputStream entrada;
    DataOutputStream salida;
    
    public Ventana() {
        
        initComponents();
        setVisible(true);
        jDesencriptar.setEnabled(false);
    }
    
    public void iniciar(InputStream e, OutputStream s){
        jTextPuntos.setVisible(true);
        jPmx.setEditable(true);
        jClave.setEditable(true);
        jButton1.setEnabled(false);
        
        entrada = new DataInputStream(new BufferedInputStream(e));
        salida = new DataOutputStream(new BufferedOutputStream(s));
    }
        

      

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPuntos = new javax.swing.JTextArea();
        jPmx = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaResultado = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jClave = new javax.swing.JTextField();
        jDesencriptar = new javax.swing.JButton();
        jPmy = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jP = new javax.swing.JTextField();
        jB = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jA = new javax.swing.JTextField();
        jGX = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jGY = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jK = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cliente");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jTextPuntos.setColumns(20);
        jTextPuntos.setRows(5);
        jScrollPane1.setViewportView(jTextPuntos);

        jPmx.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPmxActionPerformed(evt);
            }
        });

        jLabel1.setText("Texto a enviar:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel2.setText("Presiona \"Enter\" para enviar mensaje");

        jButton1.setText("Encriptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextAreaResultado.setColumns(20);
        jTextAreaResultado.setEditable(false);
        jTextAreaResultado.setRows(5);
        jScrollPane2.setViewportView(jTextAreaResultado);

        jLabel4.setText("Detalle de --------------:");

        jLabel5.setText("Clave Privada:");

        jDesencriptar.setText("Desencriptar");
        jDesencriptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDesencriptarActionPerformed(evt);
            }
        });

        jPmy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPmyActionPerformed(evt);
            }
        });

        jLabel6.setText("Pmx");

        jLabel7.setText("Pmy");

        jLabel8.setText("Punto a:");

        jB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBActionPerformed(evt);
            }
        });

        jLabel9.setText("Punto b:");

        jLabel10.setText("Modulo p:");

        jLabel11.setText("Punto Gx:");

        jLabel12.setText("Punto Gy:");

        jLabel3.setText("K:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel4)
                        .addGap(0, 491, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(40, 40, 40)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel10))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jClave, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                                            .addComponent(jP))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel11)
                                            .addComponent(jLabel8))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jA)
                                            .addComponent(jGX, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addGroup(layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel12)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jGY, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(12, 12, 12)
                                                .addComponent(jLabel9)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jB, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jPmx, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel6)
                                                .addGap(27, 27, 27)))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel7)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jPmy, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel2)
                                                .addGap(39, 39, 39)
                                                .addComponent(jLabel3)
                                                .addGap(18, 18, 18)
                                                .addComponent(jK, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(83, 83, 83)
                                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(77, 77, 77)
                                        .addComponent(jDesencriptar, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jDesencriptar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel8)
                            .addComponent(jA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jGX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(jGY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jPmx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPmy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(9, 9, 9)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Conexión al servidor al presionar "iniciar sesion"
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        a = Integer.parseInt(jA.getText().toString());
        b = Integer.parseInt(jB.getText().toString());
        p = Integer.parseInt(jP.getText().toString());
        x = Integer.parseInt(jGX.getText().toString());
        y = Integer.parseInt(jGY.getText().toString());
        k = Integer.parseInt(jK.getText().toString());
        Pmx = Integer.parseInt(jPmx.getText().toString());
        Pmy = Integer.parseInt(jPmy.getText().toString());
        privada = Integer.parseInt(jClave.getText().toString());
        int[] Pm = {Pmx,Pmy};
        
        
        if(Pmx<0 || Pmy<0){
        jTextAreaResultado.append("Debe ingresar valores en Pmx y Pmy");     
        }
        else{//else0
            
        boolean ab=cifrador.validaAB(a,b);
        if(ab==false){jTextAreaResultado.append("\nA y B no son puntos válidos");}
        else{//elseab
        jTextAreaResultado.append("\nLos puntos a:"+a+" y b:"+b+" son válidos");   
        int cantPuntos = cifrador.obtienePuntos(a,b,p);
        //////////// Validamos la clave privada ////////////
        if(!cifrador.validaPrivada(cantPuntos, privada)){
        jTextAreaResultado.append("\nLa clave privada no es válida");
        }
        else{//elseprivada
        jTextAreaResultado.append("\nLa clave privada ["+privada+"] es válida");
        
        //////////// Validamos el punto generador ////////////
        if(!cifrador.validaGenerador(x,y,a,p)){
        jTextAreaResultado.append("\nEl punto generador no es válido");
        }
        else{//elsegenerador
        jTextAreaResultado.append("\nEl punto generador ("+x+","+y+")es válido\n");
        jTextAreaResultado.append("\n====TODOS LOS DATOS SON VÁLIDOS===\n");
        
        int[][] puntos = cifrador.guardaPuntos(a, b, p);
        jTextPuntos.setText("LISTADO DE PUNTOS DE LA CURVA:\n\n");
        for(int pts=0 ; pts<cantPuntos ; pts++){
        jTextPuntos.append(pts+") EL PUNTO ("+puntos[pts][0]+","+puntos[pts][1]+") ES VÁLIDO\n");
        }
        
        jTextAreaResultado.append("\n\n====GENERANDO LA LLAVE PÚBLICA===\n");
        int publica[] = new int[2];
        publica = cifrador.clavePublica(privada,x,y,a,p);

        Cifrado = cifrador.cifraECC(x,y,a,p,k,Pm,publica);
        jTextAreaResultado.append("\nCIFRADO: { ("+Cifrado[0][0]+","+Cifrado[0][1]+"),("+Cifrado[1][0]+","+Cifrado[1][1]+") }"); 
        
        jDesencriptar.setEnabled(true);
        }//cierre elsegenerador
        }//cierre elseprivada
        }//cierre elseab
        }//cierre else0
        
    }//GEN-LAST:event_jButton1ActionPerformed
    //Esta función cierra la conexión si la ventana se cierra
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
       
    }//GEN-LAST:event_formWindowClosing
    //Si la ventana se cierra, eliminamos el hilo de ejecución
    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        System.out.println("salimos");
    }//GEN-LAST:event_formWindowClosed

    private void jPmxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPmxActionPerformed

    }//GEN-LAST:event_jPmxActionPerformed

    private void jDesencriptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDesencriptarActionPerformed

        a = Integer.parseInt(jA.getText().toString());
        b = Integer.parseInt(jB.getText().toString());
        p = Integer.parseInt(jP.getText().toString());
        x = Integer.parseInt(jGX.getText().toString());
        y = Integer.parseInt(jGY.getText().toString());
        privada = Integer.parseInt(jClave.getText().toString());
        
        Descifrado = cifrador.descifraECC(x,y,a,p,Cifrado[1],privada,k);
        jTextAreaResultado.append("\n\n====RESULTADO DE LA DESENCRIPTACIÓN===");
        jTextAreaResultado.append("\n\nDESCIFRADO: ("+Descifrado[0]+","+Descifrado[1]+")\n");
        
    }//GEN-LAST:event_jDesencriptarActionPerformed

    private void jPmyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPmyActionPerformed
        Pmy = Integer.parseInt(jPmy.getText());       
    }//GEN-LAST:event_jPmyActionPerformed

    private void jBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jBActionPerformed
    
    public static void main(String args[]) throws IOException{
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jA;
    private javax.swing.JTextField jB;
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jClave;
    private javax.swing.JButton jDesencriptar;
    private javax.swing.JTextField jGX;
    private javax.swing.JTextField jGY;
    private javax.swing.JTextField jK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jP;
    public javax.swing.JTextField jPmx;
    public javax.swing.JTextField jPmy;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextAreaResultado;
    public javax.swing.JTextArea jTextPuntos;
    // End of variables declaration//GEN-END:variables

    @Override
    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
